package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyCommunityDoorCardAddBo;
import com.ruoyi.system.bo.ZsyyCommunityDoorCardQueryBo;
import com.ruoyi.system.bo.ZsyyCommunityDoorCardEditBo;
import com.ruoyi.system.domain.ZsyyCommunityDoorCard;
import com.ruoyi.system.mapper.ZsyyCommunityDoorCardMapper;
import com.ruoyi.system.vo.ZsyyCommunityDoorCardVo;
import com.ruoyi.system.service.IZsyyCommunityDoorCardService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 门禁卡记录Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyCommunityDoorCardServiceImpl extends ServicePlusImpl<ZsyyCommunityDoorCardMapper, ZsyyCommunityDoorCard> implements IZsyyCommunityDoorCardService {
    @Autowired
    private ZsyyCommunityDoorCardMapper iZsyyCommunityDoorCardMapper;
    @Autowired
	private GetCommunityIdImpl getCommunityIdImpl;

    @Override
    public ZsyyCommunityDoorCardVo queryById(Long id){
        return getVoById(id, ZsyyCommunityDoorCardVo.class);
    }

    @Override
    public TableDataInfo<ZsyyCommunityDoorCardVo> queryPageList(ZsyyCommunityDoorCardQueryBo bo) {
    	bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        PagePlus<ZsyyCommunityDoorCard, ZsyyCommunityDoorCardVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyCommunityDoorCardVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyCommunityDoorCardVo> queryList(ZsyyCommunityDoorCardQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyCommunityDoorCardVo.class);
    }

    private LambdaQueryWrapper<ZsyyCommunityDoorCard> buildQueryWrapper(ZsyyCommunityDoorCardQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyCommunityDoorCard> lqw = Wrappers.lambdaQuery();
		bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
        lqw.eq(bo.getDoorId() != null, ZsyyCommunityDoorCard::getDoorId, bo.getDoorId());
        lqw.eq(StrUtil.isNotBlank(bo.getDeviceIp()), ZsyyCommunityDoorCard::getDeviceIp, bo.getDeviceIp());
        lqw.eq(StrUtil.isNotBlank(bo.getPort()), ZsyyCommunityDoorCard::getPort, bo.getPort());
        lqw.like(StrUtil.isNotBlank(bo.getUserName()), ZsyyCommunityDoorCard::getUserName, bo.getUserName());
        lqw.eq(bo.getCommunityId() != null, ZsyyCommunityDoorCard::getCommunityId, bo.getCommunityId());
        lqw.eq(StrUtil.isNotBlank(bo.getCardNos()), ZsyyCommunityDoorCard::getCardNos, bo.getCardNos());
        lqw.eq(bo.getCardNum() != null, ZsyyCommunityDoorCard::getCardNum, bo.getCardNum());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyCommunityDoorCardAddBo bo) {
        ZsyyCommunityDoorCard add = BeanUtil.toBean(bo, ZsyyCommunityDoorCard.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyCommunityDoorCardEditBo bo) {
        ZsyyCommunityDoorCard update = BeanUtil.toBean(bo, ZsyyCommunityDoorCard.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyCommunityDoorCard entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
